package com.googlecode.garbagecan.cxfstudy.jaxws;

import java.net.SocketTimeoutException;

import javax.xml.ws.WebServiceException;

import org.apache.cxf.endpoint.Client;
import org.apache.cxf.frontend.ClientProxy;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.cxf.transport.http.HTTPConduit;
import org.apache.cxf.transports.http.configuration.HTTPClientPolicy;

public class MyClient {
	public static void main(String[] args) throws Exception {
		long start = System.currentTimeMillis();
		test();
		long stop = System.currentTimeMillis();
		System.out.println(stop - start);
	}
	
	public static void test() throws Exception {
		JaxWsProxyFactoryBean factoryBean = new JaxWsProxyFactoryBean();
		factoryBean.setAddress("http://localhost:9000/ws/jaxws/customerService");
		factoryBean.setServiceClass(CustomerService.class);
		Object obj = factoryBean.create();
		
		Client proxy = ClientProxy.getClient(obj);
		HTTPConduit conduit = (HTTPConduit) proxy.getConduit();
		HTTPClientPolicy policy = new HTTPClientPolicy();
		policy.setConnectionTimeout(5 * 1000);
		policy.setReceiveTimeout(5 * 1000);
		conduit.setClient(policy);

		CustomerService customerService = (CustomerService) obj;
		try {
			Customer customer = customerService.findCustomer("123");
			System.out.println("Customer: " + customer);
		} catch(Exception e) {
			if (e instanceof WebServiceException 
					&& e.getCause() instanceof SocketTimeoutException) {
				System.err.println("This is timeout exception.");
			} else {
				e.printStackTrace();
			}
		}
	}
}
